from datetime import datetime

from odoo import fields, models, api, _
from odoo.exceptions import UserError, ValidationError
from odoo.tools import config

qt = False


class FinishedProductWizard(models.TransientModel):
    _name = "finished.product.wizard"
    _description = "Finished Product Wizard"

    level_num = fields.Integer(string="Level",required=True)
    is_halogen = fields.Boolean(string="Halogen")

    def confirm_action(self):
        if self.level_num>99:
            raise ValidationError(_("The number of product layers should be a 2-digit integer!"))
        level_num = "{:02d}".format(self.level_num)
        is_halogen = 'L' if self.is_halogen else 'H'
        default_code = 'W' + level_num+is_halogen+self.env['ir.sequence'].get_serial_number(code='product_template.default_code',
                                                                            prefix='',
                                                                            padding=6) + 'T0'
        uom_id = self.env['uom.uom'].sudo().search([('name', '=', 'PCS')], limit=1)
        new_product_id = self.env['product.template'].sudo().create({
            'name': default_code,
            'purchase_ok': False,
            'sale_ok': False,
            'detailed_type': 'product',
            'categ_id': self.env.ref('sb_sale.finished_product_type').id,
            'route_ids': [self.env.ref('mrp.route_warehouse0_manufacture').id],
            'tracking': 'lot',
            'uom_id': uom_id.id or 1,
            'uom_po_id': uom_id.id or 1,
            'default_code': default_code,
            'materiel_status': 'T',
        }).id


        return {
            'name': _('Product'),
            'type': 'ir.actions.act_window',
            'res_model': 'product.template',
            'domain': [('id', '=', new_product_id)],
            'view_mode': 'tree',
            'view_id': self.env.ref('sb_engineering.engineering_product_template_ext_tree_view').id,
            'target': 'current',
        }